﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraPrinting;
using System.IO;

namespace Reporting
{
    public partial class frmReportViewer : XtraForm
    {
        public DataTable Source { get; set; }

        public frmReportViewer()
        {
            InitializeComponent();
        }

        private void frmReportViewer_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = Source;
            ColumnReFormat(gridView1);
            gridView1.BestFitColumns();
        }

        public void ColumnReFormat(DevExpress.XtraGrid.Views.Grid.GridView grd)
        {
            for (int i = 0; i <= grd.Columns.Count - 1; i++)
            {
                switch (grd.Columns[i].ColumnType.ToString())
                {
                    case "System.int32":
                        grd.Columns[i].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                        grd.Columns[i].DisplayFormat.FormatString = "f0";

                        grd.Columns[i].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                        grd.Columns[i].SummaryItem.SetSummary(DevExpress.Data.SummaryItemType.Sum, "Toplam={0:f0}");

                        break;
                    case "System.int16":
                        grd.Columns[i].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                        grd.Columns[i].DisplayFormat.FormatString = "f0";

                        grd.Columns[i].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                        grd.Columns[i].SummaryItem.SetSummary(DevExpress.Data.SummaryItemType.Sum, "Toplam={0:f0}");
                        break;
                    case "System.Decimal":
                        grd.Columns[i].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                        grd.Columns[i].DisplayFormat.FormatString = "n2";

                        grd.Columns[i].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                        grd.Columns[i].SummaryItem.SetSummary(DevExpress.Data.SummaryItemType.Sum, "Toplam={0:n2}");

                        break;
                    case "System.Double":
                        grd.Columns[i].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                        grd.Columns[i].DisplayFormat.FormatString = "n2";

                        grd.Columns[i].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                        grd.Columns[i].SummaryItem.SetSummary(DevExpress.Data.SummaryItemType.Sum, "Toplam={0:n2}");

                        break;
                    case "System.Float":
                        grd.Columns[i].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                        grd.Columns[i].DisplayFormat.FormatString = "n2";

                        grd.Columns[i].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                        grd.Columns[i].SummaryItem.SetSummary(DevExpress.Data.SummaryItemType.Sum, "Toplam={0:n2}");
                        break;
                }
            }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.FileName = "";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToExcelOld(dlg.FileName);
            }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            PrintableComponentLink link = new PrintableComponentLink(new PrintingSystem());
            link.CreateMarginalHeaderArea += new CreateAreaEventHandler(Link_CreateMarginalHeaderArea);
            link.Component = gridControl1;
            link.Landscape = true;
            link.CreateDocument();
            link.PrintDlg();
        }

        private void Link_CreateMarginalHeaderArea(object sender, CreateAreaEventArgs e)
        {
            PageInfoBrick tarih = e.Graph.DrawPageInfo(PageInfo.DateTime, "", Color.Black,
               new RectangleF(0, 0, 100, 20), BorderSide.None);

            TextBrick baslik = e.Graph.DrawString(this.Text, Color.Black,
   new RectangleF(0, 74, e.Graph.ClientPageSize.Width, 20), BorderSide.None);

            if (File.Exists(Application.StartupPath + "\\rapormesaj.txt"))
            {
                string mesaj = File.ReadAllText(Application.StartupPath + "\\rapormesaj.txt");
                e.Graph.DrawString(mesaj, Color.Black, new RectangleF((e.Graph.ClientPageSize.Width / 2) + (Properties.Resources.logo.Width / 2) + 50, 0, (e.Graph.ClientPageSize.Width / 2) - (Properties.Resources.logo.Width / 2) - 50, 70), BorderSide.None);
            }

            e.Graph.DrawImage(Properties.Resources.logo, new RectangleF((e.Graph.ClientPageSize.Width / 2) - (Properties.Resources.logo.Width / 2), 0, Properties.Resources.logo.Width, Properties.Resources.logo.Height), BorderSide.None, Color.Transparent);

            tarih.LineAlignment = BrickAlignment.Near;
            tarih.Alignment = BrickAlignment.Near;
            tarih.AutoWidth = true;

            baslik.HorzAlignment = DevExpress.Utils.HorzAlignment.Near;
            baslik.VertAlignment = DevExpress.Utils.VertAlignment.Center;
        }
    }
}
